#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    umask(0);
    if (mkfifo("myfifo", 0644) < 0)
    {
        perror("mkfifo");
        exit(1);
    }
    int rfd = open("myfifo", O_RDONLY);
    if (rfd < 0)
    {
        perror("open");
        exit(2);
    }
    char buf[1024];
    while (1)
    {
        buf[0] = 0;//清空buffer
        printf("Please wait...\n");
        ssize_t s = read(rfd, buf, sizeof(buf) - 1);
        if (s > 0)
        {
            buf[s - 1] = 0;
            printf("client say# %s\n", buf);
        }
        else if (s == 0)
        {
            printf("client quit, exit now!\n");
            exit(2);
        }
        else
        {
            perror("read error");
            exit(3);
        }
    }
    close(rfd);
    return 0;
}
